package com.example.chasshelper.dao;

import com.example.chasshelper.model.Classroom;
import com.example.chasshelper.model.Registration;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.model.Test;
import com.example.chasshelper.utils.OkHttpUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SolutionDao {
    private OkHttpClient okHttpClient = OkHttpUtils.getClient();
    private String ipUrl = OkHttpUtils.getUrl();
    private String url;
    /*
     * 添加
     * */
    public  void insert(Classroom classroom) {
        url = ipUrl + "/solution/insert?classroomId=" + classroom.getClassroomId();
        System.out.println("url:"+url);
        Request request = new Request.Builder().url(url).get().build();
        final Map<String, Object> resultMap = new HashMap<>();
        //执行异步get请求
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println("成功");
            }
        });
    }
    /*
     * 提交答案
     * */
    public  void submit(Classroom classroom, Student student, Test test,String answer,String type) {
        url = ipUrl + "/solution/update?classroomId="+classroom.getClassroomId()+"&studentId="+student.getStudentId()+"&testId="+test.getTestId()+"&solutionStudentAnswer="+answer+"&type="+type;
        System.out.println("url:"+url);
        Request request = new Request.Builder().url(url).get().build();
        final Map<String, Object> resultMap = new HashMap<>();
        //执行异步get请求
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println("成功");
            }
        });
    }

}
